package org.PISS.DAO;

import org.PISS.controller.model.Fitter;
import org.PISS.controller.model.Supplier;
import org.PISS.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

/**
 * Created by admin on 25.01.14.
 */
public class FitterDAO {
    public List<Fitter> getFitterList(){
        Session session = null;
        List<Fitter> fitterList=null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query query = session.createQuery("FROM Fitter ");
            //noinspection unchecked
            fitterList=query.list();
        }catch (Exception e){
            throw new RuntimeException("Save exception",e);
        }finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return fitterList;
    }

    public void addFitter(Fitter fitter){
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(fitter);
            session.getTransaction().commit();
        } catch (Exception e) {
            throw new RuntimeException("Save exception",e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
}
